package graphics.screens;

import files.Files;
import graphics.BootColor;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import tool.Properties;
import tool.ScreenUtils;

import boot.Boot;

public class StatsScreen extends Canvas implements CommandListener {

	private final Command okCommand = new Command("OK", Command.OK, 1);
	private static final int SCREEN_WIDTH = ScreenUtils.SCREEN_WIDTH;

	private int upOffset;
	private int fontHeight;
	private int boundOffset = 10;
	private Graphics graphics;
	private Properties bootProps;

	public StatsScreen() {
		setup();
	}

	public void setup() {
		bootProps = new Properties(Files.BOOT_ROOT
				+ "/DBProperties/Boot.db.properties");
		addCommand(okCommand);
		setCommandListener(this);
	}

	protected void paint(Graphics someGraphics) {
		graphics = someGraphics;
		fontHeight = graphics.getFont().getHeight();
		upOffset = graphics.getFont().getHeight() / 2;
		graphics.setColor(BootColor.BLACK);
		graphics.fillRect(0, 0, SCREEN_WIDTH, 250);
		graphics.setColor(BootColor.WHITE);

		paintDuration(upOffset);
		paintCollectionVolume(upOffset + fontHeight);

		paintCollectionSize(upOffset + fontHeight * 3);
		paintBootSize(upOffset + fontHeight * 4);
		paintNowSize(upOffset + fontHeight * 5);

		paintBars(upOffset + fontHeight * 7);
	}

	private void paintNowSize(int offset) {
		
		graphics.drawString("Now size", boundOffset, offset, Graphics.TOP
				| Graphics.LEFT);
		String sizeText = bootProps.getProperty("nowSize");
		
		int size = Integer.parseInt(sizeText);
		String roundSize = "~ " + (size / 10 * 10);
		
		graphics.drawString(roundSize, SCREEN_WIDTH - boundOffset, offset,
				Graphics.TOP | Graphics.RIGHT);
	}

	private void paintBootSize(int offset) {
		
		graphics.drawString("Boot size", boundOffset, offset, Graphics.TOP
				| Graphics.LEFT);
		String sizeText = bootProps.getProperty("bootSize");
		
		int size = Integer.parseInt(sizeText);
		String roundSize = "~ " + (size / 100 * 100);
		
		graphics.drawString(roundSize, SCREEN_WIDTH - boundOffset, offset,
				Graphics.TOP | Graphics.RIGHT);
	}

	private void paintCollectionSize(int offset) {
		graphics.drawString("Total size", boundOffset, offset,
				Graphics.TOP | Graphics.LEFT);
		String sizeText = bootProps.getProperty("num");
		
		int size = Integer.parseInt(sizeText);
		String roundSize = "> " + (size / 500 * 500);
		
		graphics.drawString(roundSize, SCREEN_WIDTH - boundOffset, offset,
				Graphics.TOP | Graphics.RIGHT);
	}

	private void paintCollectionVolume(int offset) {
		String sizeOnDisk = bootProps.getProperty("size");
		graphics.drawString(sizeOnDisk, SCREEN_WIDTH / 2, offset, Graphics.TOP
				| Graphics.HCENTER);
	}

	private void paintDuration(int offset) {
		String duration = bootProps.getProperty("numDays");
		graphics.drawString(duration, SCREEN_WIDTH / 2, offset, Graphics.TOP
				| Graphics.HCENTER);
	}

	private void paintBars(int offset) {
		
		int nowSize = Integer.parseInt(bootProps.getProperty("nowSize"));
		int bootSize = Integer.parseInt(bootProps.getProperty("bootSize"));
		int collectionSize = Integer.parseInt(bootProps.getProperty("num"));
		int gap = 5;
		int barsWidth = SCREEN_WIDTH - boundOffset * 2;
		int innerBarsWidth = barsWidth - gap * 2;

		int nowWidth = innerBarsWidth * nowSize / collectionSize;
		int remBootWidth = innerBarsWidth * bootSize / collectionSize
				- nowWidth;
		int remCollectionWidth = innerBarsWidth - nowWidth - remBootWidth;
		int arclength = 3;
		int barHeight = fontHeight;

		graphics.setColor(BootColor.YELLOW);
		graphics.fillRoundRect(boundOffset, offset, remCollectionWidth,
				barHeight, arclength, arclength);
		graphics.setColor(BootColor.FLASHY_MENU_COLOR);
		graphics.fillRoundRect(boundOffset + remCollectionWidth + gap, offset,
				remBootWidth, barHeight, arclength, arclength);
		graphics.setColor(BootColor.FLASHY_MENU_COLOR_2);
		graphics.fillRoundRect(boundOffset + remCollectionWidth + remBootWidth
				+ gap * 2, offset, nowWidth, barHeight, arclength, arclength);
	}

	public void commandAction(Command command, Displayable disp) {

		if (command == okCommand) {
			Boot.getInstance().readyToGo();
		}
	}

}
